package ui;

import bot.ScriptMethods;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.PrintStream;
import java.lang.reflect.Method;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JOptionPane;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;

public class ScriptUI extends JFrame {

	private File dir = new File("./scripts/");
	private File dir2 = new File("./");
	private JList scriptList;
	private JButton startButton;
	public boolean hasStarted = false;

	private ArrayList<String> scripts = new ArrayList<String>();

	private void loadScripts() {
		String[] files = dir.list();
		for (int i = 0; i < files.length; i++) {
			try {
				if (files[i].endsWith(".java") || files[i].endsWith(".ini") || files[i].contains("$")) {
					scripts.remove(i);
				} else {
					System.out.println("Loaded script: " + files[i]);
					scripts.add(files[i].replace(".class", ""));
				}
			} catch (Exception ex) { }
		}
	}

	public void initComponents() {

		startButton = new JButton();
		scriptList = new JList();

		scripts.clear();

		loadScripts();

		scriptList.setListData(scripts.toArray());

		setTitle("Run script");
		setDefaultCloseOperation(2);
		setResizable(false);

		Container container = getContentPane();
		container.setLayout(null);
		container.setPreferredSize(new Dimension(300, 315));

		JScrollPane scriptScroll = new JScrollPane(scriptList);
		scriptScroll.setBounds(new Rectangle(new Point(3, 3), new Dimension(293, 279)));
		container.add(scriptScroll);

		startButton.setText("Start");
		startButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionEvent) {
				startButtonActionPerformed();
			}
		});
		startButton.setBounds(new Rectangle(new Point(3, 288), new Dimension(80, 23)));
		container.add(startButton);

		JButton reload = new JButton();
		reload.setText("Refresh");
		reload.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent actionEvent) {
				refreshButtonActionPerformed();
			}
		});
		reload.setBounds(new Rectangle(new Point(85, 288), new Dimension(80, 23)));
		container.add(reload);

		scriptList.clearSelection();

		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		pack();
		setResizable(false);
		setVisible(true);
		setLocationRelativeTo(null);
		hasStarted = true;

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.out.println("Hiding script window.");
				setVisible(false);
			}
		});
	}

	public URLClassLoader scriptLoader;
	public Class scriptClass;
	public Object script;

	ScriptMethods scriptMethods;
	@SuppressWarnings("unchecked")
	private void startButtonActionPerformed() {
		try {

			if (scriptList.getSelectedValue() != null) {

				ScriptMethods scriptMethods = new ScriptMethods();
				scriptMethods.runningScript = (String)scriptList.getSelectedValue();
				scriptMethods.debug("Running script: " + scriptMethods.runningScript);

				URL[] loadURL = new URL[] { dir2.toURI().toURL() };
				scriptLoader = new URLClassLoader(loadURL);
				scriptClass = scriptLoader.loadClass("scripts." + scriptMethods.runningScript);
				script = scriptClass.newInstance();

				Method m = scriptClass.getDeclaredMethod("start");
				m.setAccessible(true);
				m.invoke(script);
				scriptList.clearSelection();
				dispose();

			} else {
				JOptionPane.showMessageDialog(null, "You either have no script selected, or need to restart the bot.", "Null value", JOptionPane.PLAIN_MESSAGE);
			}

		} catch (Exception e) { e.printStackTrace(); }
	}

	@SuppressWarnings("unchecked")
	public void stopScript() {
		try {
			Method m = scriptClass.getDeclaredMethod("stop");
			m.setAccessible(true);
			m.invoke(script);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private void refreshButtonActionPerformed() {
		scripts.clear();
		loadScripts();
		scriptList.setListData(scripts.toArray());
	}
}